package yd.launcher;

import yd.launcher.service.Action;
import yd.launcher.service.Panel;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * 用于显示一个带有多个action按钮的panel
 */
public class ButtonsPanel extends JPanel{
    private TitledBorder titledBorder;

    private Panel panel;

    public ButtonsPanel(Panel panel) {
        this.panel = panel;
        initView();
        initPopupMenu();
        initContent();
    }

    private void initView(){
        Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        //Border etchedBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, Color.RED);
        titledBorder = BorderFactory.createTitledBorder(etchedBorder, panel.getName());
        setBorder(titledBorder);
        ((FlowLayout)getLayout()).setAlignment(FlowLayout.LEFT);
    }

    private void initPopupMenu(){
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem menuItem = new JMenuItem("Add");
        popupMenu.add(menuItem);
        setComponentPopupMenu(popupMenu);
        menuItem.addActionListener(e -> new ActionEditDialog(ButtonsPanel.this));
    }

    private void initContent(){
        for (int i = 0; i < panel.getActions().size(); i++) {
            renderButton(panel.getActions().get(i).getName(), i);
        }
        updateUI();
    }

    public void refreshPanel() {
        titledBorder.setTitle(panel.getName());
    }

    private void renderButton(String name, int serialNumber){
        JButton button = new JButton(name);
        button.setActionCommand(String.valueOf(serialNumber));
        button.addActionListener(e -> {
            Action action = panel.getActions().get(serialNumber);
            try {
                action.exec();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(ButtonsPanel.this,ex.getMessage());
            }
        });

        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem menuItem = new JMenuItem("Edit");
        JMenuItem menuItem2 = new JMenuItem("Delete");
        popupMenu.add(menuItem);
        popupMenu.add(menuItem2);
        button.setComponentPopupMenu(popupMenu);
        add(button);
        menuItem.addActionListener(e -> new ActionEditDialog(button));
        menuItem2.addActionListener(e -> {
            int option = JOptionPane.showConfirmDialog(ButtonsPanel.this, "确定删除？","选择一个选项",JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.OK_OPTION) {
                removeAction(serialNumber);
            }
        });
    }

    public void addNewAction(Action action){
        renderButton(action.getName(), panel.getActions().size());
        panel.getActions().add(action);
        updateUI();
    }

    public void removeAction(int serialNumber){
        this.removeAll();
        panel.getActions().remove(serialNumber);
        initContent();
    }

    public void editAction(int serialNumber, Action action){
        panel.getActions().set(serialNumber, action);
    }

    public Action getAction(int serialNumber) {
        return panel.getActions().get(Integer.valueOf(serialNumber));
    }
}
